package com.ehuoyun.ecstore.action;

import java.io.IOException;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

import org.apache.commons.io.IOUtils;

import com.ehuoyun.ecstore.biz.AdvertisementMgr;
import com.ehuoyun.ecstore.biz.CompanyMgr;
import com.ehuoyun.ecstore.model.Advertisement;
import com.ehuoyun.ecstore.model.Company;
import com.google.appengine.api.datastore.Blob;

public class AdvertisementActionBean extends AbstractActionBean {
	private FileBean file;
	
    @ValidateNestedProperties({
        @Validate(field="name", required=true)
    })
	private Advertisement ad;
	
    @SpringBean
    private AdvertisementMgr adMgr;
    
    @SpringBean
    private CompanyMgr companyMgr;
    
    private String adOwnerName;
    
    private String adClientName;
	
	@DefaultHandler
    @DontValidate
    public Resolution view() {
		ad = adMgr.getAdvertisement(ad.getId());
		wire();
		return new ForwardResolution("/view-ad.jsp");
	}	
	
    @DontValidate
    public Resolution edit() {
		ad = adMgr.getAdvertisement(ad.getId());
		wire();
		return new ForwardResolution("/edit-ad.jsp");
	}

//	@RolesAllowed("Authenticated")
    public Resolution save() {
		Blob image;
		try {
			if(file != null){
				image = new Blob(IOUtils.toByteArray(file.getInputStream()));
				ad.setImage(image);
			}
			if(getContext().getUser() != null) {
				ad.setCreatedBy(getContext().getUser().getId());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		adMgr.save(ad);
		return new RedirectResolution("/index.jsp");
    }
    
    private void wire() {
    	Company company;
    	if(ad != null && ad.getOwner() != null) {
    		company = companyMgr.get(ad.getOwner());
    		if(company != null) {
    			this.adOwnerName = company.getName();
    		}
    	}
    	if(ad != null && ad.getAdClient() != null) {
    		company = companyMgr.get(ad.getAdClient());
    		if(company != null) {
    			this.adClientName = company.getName();
    		}
    	}
    }

    public FileBean getFile() {
		return file;
	}

	public void setFile(FileBean file) {
		this.file = file;
	}

	public Advertisement getAd() {
		return ad;
	}

	public void setAd(Advertisement ad) {
		this.ad = ad;
	}

	public String getAdOwnerName() {
		return adOwnerName;
	}

	public void setAdOwnerName(String adOwnerName) {
		this.adOwnerName = adOwnerName;
	}

	public String getAdClientName() {
		return adClientName;
	}

	public void setAdClientName(String adClientName) {
		this.adClientName = adClientName;
	}

}
